import axios from "axios";
import { Message } from 'element-ui';

const service = axios.create({
  baseURL: '/api',
  timeout: 5000
})
service.interceptors.request.use(config => {
  return config
}, error => {
  return Promise.reject(error)
})
// 响应拦截
service.interceptors.response.use(conf => {
  if (conf.data.rescode !== 2000) {
    Message({
      message: conf.data.message,
      type: 'error'
    });
    // return conf.data
  }
  return conf.data
}, err => {
  // 默认值
  let rescode = 2000
  let message = "网络错误"
  if (err.response) {
    // 如果有返回值，则报返回值的错误
    rescode = err.response.data.rescode
    message = err.response.data.message
  }
  Message({
    message: message,
    type: 'error'
  });
  console.log(err);

  return { rescode, message }
})
export default service


